package com.zzc.qqchat.controller;


import com.zzc.qqchat.repository.AddFriendsListRep;
import com.zzc.qqchat.repository.FriendsRep;
import com.zzc.qqchat.repository.LoginRespository;
import com.zzc.qqchat.repository.UserInforRep;
import com.zzc.qqchat.domain.*;
import com.zzc.qqchat.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@Controller
public class AddFriendsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Resource
    FriendsRep friendsRep;

    @Resource
    AddFriendsListRep addFriendsListRep;

    @Resource
    LoginRespository loginRespository;

    @Resource
    UserInforRep userInforRep;

    @ResponseBody
    @PostMapping("/addfriends")
    public String addFriends(int otherSideId,int userId,String userName){

        Optional<UserInfor> have = userInforRep.findById(otherSideId);
        if(have.isPresent()){
            Time time= new Time(0);
            Message message =new Message("",userId,otherSideId,time,userName,"addfriends");
            simpMessagingTemplate.convertAndSendToUser(String.valueOf(otherSideId),"/message/get",message);
            AddFriendsList addList = new AddFriendsList();
            addList.setReceiverId(otherSideId);
            addList.setSenderId(userId);
            addList.setSenderName(userName);
            addFriendsListRep.save(addList);
            return "已发送请求！";
        }else{
            return "用户不存在！";
        }

    }

    @ResponseBody
    @PostMapping("/access")
    public void access(int otherSideId,int userId){

        List<Friends> fr = friendsRep.findAll();
        Friends friends = new Friends();
        friends.setId(fr.size());
        friends.setFriendId(otherSideId);
        friends.setUserId(userId);
        friendsRep.save(friends);
        friends.setId(fr.size()+1);
        friends.setFriendId(userId);
        friends.setUserId(otherSideId);
        friendsRep.save(friends);

        ArrayList<UserInfor> myInfor = loginRespository.findByUserId(userId);
        ArrayList<UserInfor> friendInfor = loginRespository.findByUserId(otherSideId);
        Time time= new Time(0);
        Message message =new Message(myInfor.get(0).getPicture(),userId,otherSideId,time,myInfor.get(0).getUserName(),"access");
        simpMessagingTemplate.convertAndSendToUser(String.valueOf(otherSideId),"/message/get",message);
        Message message1 =new Message(friendInfor.get(0).getPicture(),otherSideId,userId,time,friendInfor.get(0).getUserName(),"access");
        simpMessagingTemplate.convertAndSendToUser(String.valueOf(userId),"/message/get",message1);
        addFriendsListRep.deleteByReceAndSenderId(userId,otherSideId);
    }

    @ResponseBody
    @PostMapping("/refuse")
    public void refuse(int otherSideId,int userId){

        ArrayList<UserInfor> myInfor = loginRespository.findByUserId(userId);
        Time time= new Time(0);
        Message message =new Message("",userId,otherSideId,time,myInfor.get(0).getUserName(),"refuse");
        simpMessagingTemplate.convertAndSendToUser(String.valueOf(otherSideId),"/message/get",message);
        addFriendsListRep.deleteByReceAndSenderId(userId,otherSideId);
    }

    @ResponseBody
    @PostMapping("/addlist")
    public ArrayList<AddFriendsList> addlist(int userId){
           ArrayList<AddFriendsList> thelist = addFriendsListRep.findByReceiverId(userId);
           return thelist;
    }
}
